package com.wj.tool.common.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;

import java.io.IOException;
import java.text.SimpleDateFormat;

public final class JsonUtils {
    private static final Logger log = LoggerFactory.getLogger(JsonUtils.class);
    private static final ObjectMapper MAPPER = new ObjectMapper();


    public static <T> String objectToString(T object) {
        String json = "";

        try {
            MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
            MAPPER.setDateFormat(new SimpleDateFormat("yyyyMMddHHmmss"));
            json = MAPPER.writeValueAsString(object);
        } catch (Exception var3) {
            log.warn("An error occurred while object convert to json ：", var3);
        }

        return json;
    }

    public static <T> JSONObject objectToJson(T object) throws JSONException {
        String jsonStr = "";

        try {
            jsonStr = MAPPER.writeValueAsString(object);
        } catch (IOException var3) {
            log.error("An error occurred while object convert to json ：", var3);
        }

        return new JSONObject(jsonStr);
    }

    public static <T> byte[] objectToByte(T object) throws JsonProcessingException {
        byte[] result = new byte[0];

        try {
            result = MAPPER.writeValueAsBytes(object);
        } catch (JsonProcessingException var3) {
            log.warn("An error occurred while object convert to byte ：", var3);
        }

        return result;
    }


    private JsonUtils() {
    }
}